// 过滤器
export default {
    /*去除空格 
        type 1-所有空格 2-前后空格 3-前空格 4-后空格
        {{ string | trim(1) }} //去除所有空格，过滤器第⼀个参数为value
    */
    trim(value, trim) {
        switch (trim) {
            case 1:
                return value.replace(/\s+/g, "");
            case 2:
                return value.replace(/(^\s*)|(\s*$)/g,
                    "");
            case 3:
                return value.replace(/(^\s*)/g, "");
            case 4:
                return value.replace(/(\s*$)/g, "");
            default:
                return value;
        }
    },
    /*
    任意格式期处理
    //使格式：
    // {{ '2018-09-14 01:05' | formaDate(yyyy-MM-dd hh:mm:ss) }} 
    // {{ '2018-09-14 01:05' | formaDate(yyyy-MM-dd) }} 
    // {{ '2018-09-14 01:05' | formaDate(MM/dd) }} 等
    */
    formaDate(value, fmt) {
        var date = new Date(value);
        var o = {
            "M+": date.getMonth() + 1, //⽉份
            "d+": date.getDate(), //⽇
            "h+": date.getHours(), //⼩时
            "m+": date.getMinutes(), //分
            "s+": date.getSeconds(), //秒
            "w+": date.getDay(), //星期
            "q+": Math.floor((date.getMonth() + 3) / 3), // 季度 
            "S": date.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o) {
            if (k === 'w+') {
                if (o[k] === 0) {
                    fmt = fmt.replace('w', '周⽇');
                } else if (o[k] === 1) {
                    fmt = fmt.replace('w', '周⼀');
                } else if (o[k] === 2) {
                    fmt = fmt.replace('w', '周⼆');
                } else if (o[k] === 3) {
                    fmt = fmt.replace('w', '周三');
                } else if (o[k] === 4) {
                    fmt = fmt.replace('w', '周四');
                } else if (o[k] === 5) {
                    fmt = fmt.replace('w', '周五');
                } else if (o[k] === 6) {
                    fmt = fmt.replace('w', '周六');
                }
            } else if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" +
                    o[k]).length)));
            }
        }
        return fmt;
    },
    //字⺟⼤⼩写切换
    /*type
     1:首字母大写
     2：首字母小写
     3：大小写转换
     4：全部大写
     5：全部小写
     * */
    changeCase(str, type) {
        function ToggleCase(str) {
            var itemText = ""
            str.split("").forEach(
                function(item) {
                    if (/^([a-z]+)/.test(item)) {
                        itemText += item.toUpperCase();
                    } else if (/^([A-Z]+)/.test(item)) {
                        itemText += item.toLowerCase();
                    } else {
                        itemText += item;
                    }
                });
            return itemText;
        }
        switch (type) {
            case 1:
                return str.replace(/\b\w+\b/g, function(word) {
                    return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();
                });
            case 2:
                return str.replace(/\b\w+\b/g, function(word) {
                    return word.substring(0, 1).toLowerCase() + word.substring(1).toUpperCase();
                });
            case 3:
                return ToggleCase(str);
            case 4:
                return str.toUpperCase();
            case 5:
                return str.toLowerCase();
            default:
                return str;
        }
    },
    //字符串替换
    replaceAll(str, AFindText, ARepText) {
        raRegExp = new RegExp(AFindText, "g");
        return str.replace(raRegExp, ARepText);
    },
    //现金额大写转换函数
    //ecDo.upDigit(168752632)
    //result："⼈⺠币壹亿陆仟捌佰柒拾伍万贰仟陆佰叁拾贰元整"
    //ecDo.upDigit(1682)
    //result："⼈⺠币壹仟陆佰捌拾贰元整"
    //ecDo.upDigit(-1693)
    //result："⽋⼈⺠币壹仟陆佰玖拾叁元整"
    upDigit(n) {
        var fraction = ['⻆', '分', '厘'];
        var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
        var unit = [
            ['元', '万', '亿'],
            ['', '拾', '佰', '仟']
        ];
        var head = n < 0 ? '欠人名币' : '人名币';
        n = Math.abs(n);
        var s = '';
        for (var i = 0; i < fraction.length; i++) {
            s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
        }
        s = s || '整';
        n = Math.floor(n);
        for (var i = 0; i < unit[0].length && n > 0; i++) {
            var p = '';
            for (var j = 0; j < unit[1].length && n > 0; j++) {
                p = digit[n % 10] + unit[1][j] + p;
                n = Math.floor(n / 10);
            }
            s = p.replace(/(零.)*零$/,
                '').replace(/^$/, '零') + unit[0][i] + s;
            //s = p + unit[0][i] + s;
        }
        return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整 ');
    },
    //保留2位小数
    toDecimal2(x) {
        var f = parseFloat(x);
        if (isNaN(f)) {
            return false;
        }
        var f = Math.round(x * 100) / 100;
        var s = f.toString();
        var rs = s.indexOf('.');
        if (rs < 0) {
            rs = s.length;
            s += '.';
        }
        while (s.length <= rs + 2) {
            s += '0';
        }
        return s;
    },
    //富文本图片处理
    normalRichText(content) {
        let ret = '';
        let keys = '/Public/upload/';
        if (!content) {
            return content;
        }
        ret = content.replace(/<img/g, '<img style=" width:100% !important" class="rich-img" ');
       ret= ret.replace(new RegExp(keys,'g'),config.http+keys);
        ret = ret.replace(/<table/g, '<table style="width:100% !important" ');
        return ret;
    },
    //富文本图片处理
    normalRichText(content) {
        let ret = '';
        let keys = '/Public/upload/';
        if (!content) {
            return content;
        }
        ret = content.replace(/<img/g, '<img style=" width:100% !important" class="rich-img" ');
       ret= ret.replace(new RegExp(keys,'g'),config.http+keys);
        ret = ret.replace(/<table/g, '<table style="width:100% !important" ');
        return ret;
    },
    //时间戳转日期
    timetrans(date, isAll = false) {
        var date = parseInt(date);
        date = date.toString().length == 13 ? new Date(date) : new Date(date * 1000); //如果date为13位不需要乘1000	
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
        if (isAll) {
            var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
            var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
            var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
            return Y + M + D + h + m + s;
        } else {
            return Y + M + D;
        }
    },
}
